function Node(val) {
    this.value = val;
    this.left = null;
    this.right = null;
}

const a1 = new Node('a');
const b1 = new Node('b');
const c1 = new Node('c');
const d1 = new Node('d');
const e1 = new Node('e');
const f1 = new Node('f');
const g1 = new Node('g');
const h1 = new Node('h');

a1.left = b1;
a1.right = c1;
b1.left = d1;
b1.right = e1;
c1.left = f1;
c1.right = g1;
e1.left = h1;

const a2 = new Node('a');
const b2 = new Node('b');
const c2 = new Node('c');
const d2 = new Node('d');
const e2 = new Node('e');
const f2 = new Node('f');
const g2 = new Node('g');
const h2 = new Node('h1');

a2.left = b2;
a2.right = c2;
b2.left = d2;
b2.right = e2;
c2.left = f2;
c2.right = g2;
e2.left = h2;

function compareTree(a1, a2) {
    if (a1 == a2) return true;
    if (a1 == null && a2 != null || a1 != null && a2 == null) return false;
    if (a1.value != a2.value) return false;
    if (a1 == null && a2 == null) return true;
    let left = compareTree(a1.left, a2.left);
    let right = compareTree(a1.right, a2.right);
    return left && right
}

// 左右子树互换的情况
// function compareTree(a1, a2) {
//     if (a1 == a2) return true;
//     if (a1 == null && a2 != null || a1 != null && a2 == null) return false;
//     if (a1.value != a2.value) return false;
//     if (a1 == null && a2 == null) return true;
//     return compareTree(a1.left, a2.left) && compareTree(a1.right, a2.right) || compareTree(a1.left, a2.right) && compareTree(a1.right, a2.left)
// }

console.log(compareTree(a1, a2));